#include "TerrainDimensionsGUI.h"
#include "ui_TerrainDimensionsGUI.h"
#include <iostream>
TerrainDimensionsGUI::TerrainDimensionsGUI(QWidget *parent) : QWidget(parent), m_ui(new Ui::TerrainDimensionsGUI)
{
    m_ui->setupUi(this);
    this->mustLockAspectRatio = false;
    lastWidth = 0.1;
    lastHeight = 0.1;
    lastLength = 0.1;
}

TerrainDimensionsGUI::~TerrainDimensionsGUI()
{
    delete m_ui;
}

void TerrainDimensionsGUI::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void TerrainDimensionsGUI::blockChildrenSignals(bool lock)
{
    m_ui->widthSpinBox->blockSignals(lock);
    m_ui->heightSpinBox->blockSignals(lock);
    m_ui->lengthSpinBox->blockSignals(lock);
}

void TerrainDimensionsGUI::setNewDimensions(float width,float length,float height)
{
    blockChildrenSignals(true);
    m_ui->widthSpinBox->setValue(width);
    m_ui->lengthSpinBox->setValue(length);
    m_ui->heightSpinBox->setValue(height);
    blockChildrenSignals(false);
    updateLastSize();
}

void TerrainDimensionsGUI::updateLastSize()
{
    this->lastWidth = m_ui->widthSpinBox->value();
    this->lastHeight = m_ui->heightSpinBox->value();
    this->lastLength = m_ui->lengthSpinBox->value();
}

void TerrainDimensionsGUI::widthChanged(double width)
{
    if(mustLockAspectRatio)
    {
        double changeRatio = width / lastWidth;
        blockChildrenSignals(true);
        double length = m_ui->lengthSpinBox->value();
        m_ui->lengthSpinBox->setValue(length * changeRatio);
        double height = m_ui->heightSpinBox->value();
        m_ui->heightSpinBox->setValue(height * changeRatio);
        blockChildrenSignals(false);
    }
    updateLastSize();
    emit dimensionsChanged(lastWidth,lastLength,lastHeight);
}

void TerrainDimensionsGUI::lengthChanged(double length)
{
    if(mustLockAspectRatio)
    {
        double changeRatio = length / lastLength;
        blockChildrenSignals(true);
        double width = m_ui->widthSpinBox->value();
        m_ui->widthSpinBox->setValue(width * changeRatio);
        double height = m_ui->heightSpinBox->value();
        m_ui->heightSpinBox->setValue(height * changeRatio);
        blockChildrenSignals(false);
    }
    updateLastSize();
    emit dimensionsChanged(lastWidth,lastLength,lastHeight);
}
void TerrainDimensionsGUI::heightChanged(double height)
{
    if(mustLockAspectRatio)
    {
        double changeRatio = height / lastHeight;
        blockChildrenSignals(true);
        double width = m_ui->widthSpinBox->value();
        m_ui->widthSpinBox->setValue(width * changeRatio);
        double length = m_ui->lengthSpinBox->value();
        m_ui->lengthSpinBox->setValue(length * changeRatio);
        blockChildrenSignals(false);
    }
    updateLastSize();
    emit dimensionsChanged(lastWidth,lastLength,lastHeight);
}

void TerrainDimensionsGUI::setAspectRatioLock(bool lock)
{
    this->mustLockAspectRatio = lock;
}

